/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.clarice.homechef.controllers;

import com.clarice.homechef.constants.Constants;
import com.clarice.homechef.domain.Meal;
import com.clarice.homechef.domain.MealReview;
import com.clarice.homechef.service.MealService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Akshay Velhal
 */
@RestController
@RequestMapping("/api")
public class MealController {

    @Autowired
    MealService mealService;

    @RequestMapping(value = "/meal", method = RequestMethod.POST)
    public @ResponseStatus(value = HttpStatus.CREATED)
    Meal addNewMeal(
            HttpServletResponse response,
            @RequestHeader(Constants.NT_LOGIN) String userName,
            @RequestBody Meal meal) throws Exception {

        System.out.println("Post: create new Meal for " + userName);
        Meal createdMeal = mealService.createNewMeal(meal, userName);
        System.out.println("Returning new Meal for " + userName + " and Meal: " + createdMeal);
        return createdMeal;
    }

    @RequestMapping(value = "/meal/{mealId}", method = RequestMethod.GET)
    public Meal getMeal(
            HttpServletResponse response,
            @RequestHeader(Constants.NT_LOGIN) String userName,
            @PathVariable int mealId) throws Exception {

        System.out.println("Get: create new Meal for " + userName);
        Meal meal = mealService.getMeal(mealId);
        System.out.println("Returning new Meal for " + userName + " and Meal: " + meal);
        return meal;
    }

    @RequestMapping(value = "/allmeals", method = RequestMethod.GET)
    public List<Meal> getAllMeals(
            HttpServletResponse response,
            @RequestHeader(Constants.NT_LOGIN) String userName,
            @RequestParam String date) throws Exception {
        System.out.println("Get: All meals " + userName);
        List<Meal> meals = mealService.getMeals(date);
        System.out.println("Returning all meals for " + userName + " and Meal: " + meals);
        return meals;
    }

    @RequestMapping(value = "/populardishes/{mealid}", method = RequestMethod.GET)
    public List<Meal> getPopularDishes(
            HttpServletResponse response,
            @RequestHeader(Constants.NT_LOGIN) String userName,
            @PathVariable int mealid) throws Exception {
        System.out.println("Get: Popular meals " + userName);
        List<Meal> meals = mealService.getPopularDishes(mealid);
        System.out.println("Returning popular meals for " + userName + " and Meal: " + meals);
        return meals;
    }

    @RequestMapping(value = "/latesttrending/{mealid}", method = RequestMethod.GET)
    public List<Meal> getLatestTrendingDishes(
            HttpServletResponse response,
            @RequestHeader(Constants.NT_LOGIN) String userName,
            @PathVariable int mealid) throws Exception {
        System.out.println("Get: Latest Trending meals " + userName);
        List<Meal> meals = mealService.getLatestTrendingDishes(mealid);
        System.out.println("Returning latest trending meals for " + userName + " and Meal: " + meals);
        return meals;
    }
    
    @RequestMapping(value = "/mealreview/{mealid}", method = RequestMethod.POST)
    public @ResponseStatus(value = HttpStatus.CREATED)
    void mealReview(
            HttpServletResponse response,
            @RequestHeader(Constants.NT_LOGIN) String userName,
            @PathVariable int mealid,
            @RequestBody MealReview mealReview) throws Exception {
        System.out.println("Post: create new Meal review for " + userName);
        mealService.addMealReview(mealid, mealReview);
    }
    
}
